<?php
class Pengumuman extends CI_Controller{
    function Pengumuman(){
        parent::__construct();
        if(!$this->session->userdata('logged_in')) redirect('auth/login');
        $this->load->library('form_validation');
        $this->load->model('pengumuman_model');
    }
    
    function index(){
        $this->tambah_pengumuman();
    }
    
    function tambah_pengumuman(){
        $sitedata['title'] = 'Tambah Pengumuman';
        //rules
        $this->form_validation->set_rules('judul', 'Judul', 'required');
        $this->form_validation->set_rules('deskripsi', 'Deskripsi', 'required');
        $this->form_validation->set_rules('mulai', 'Waktu Mulai', 'required');
        $this->form_validation->set_rules('selesai', 'Waktu Selesai', 'required');
        
        $this->form_validation->set_message('required', '%s harus diisi');
        $this->form_validation->set_error_delimiters('<div class="error">', '</div>');
        
        //upload configuration
        $config['upload_path'] = './uploads/';
        $config['max_size']	= '0';
        $config['allowed_types'] = '*';
        $this->load->library('upload', $config);
        $filepath = 'userfile';
        
        //upload file
        if($this->form_validation->run() == TRUE){
            if( !$this->upload->do_upload($filepath) == TRUE){
                $error = array('error' => $this->upload->display_errors());
                $this->load->view('pengumuman/upload_error', $error);
            }
            else{
                $judul = $this->input->post('judul');
                $deskripsi = $this->input->post('deskripsi');
                $mulai = (string)$this->input->post('mulai')." ".(string)$this->input->post('jammulai').":".(string)$this->input->post('menitmulai');
                $selesai = (string)$this->input->post('selesai')." ".(string)$this->input->post('jamselesai').':'.(string)$this->input->post('menitselesai');
                $id_jenis = $this->input->post('jenis');
                $id_pengisi = $this->session->userdata('user_id');
                $id_status = '0';
                date_default_timezone_set('Asia/Jakarta');
                $timestamp = date('Y-m-d H:i:s');
                
                $uploaddata = $this->upload->data();
                $namafile = $uploaddata['file_name'];
                
                $data = array(
                    'JUDUL_PENGUMUMAN' => $judul,
                    'DESKRIPSI_PENGUMUMAN' => $deskripsi,
                    'MULAI_TAYANG_PENGUMUMAN' => $mulai,
                    'SELESAI_TAYANG_PENGUMUMAN' => $selesai,
                    'ID_JENIS_PENGUMUMAN' => $id_jenis,
                    'LOKASI_FILE_PENGUMUMAN' => $namafile,
                    'ID_PENGISI_PENGUMUMAN' => $id_pengisi,
                    'ID_STATUS_PENGUMUMAN' => $id_status,
                    'TIMESTAMP_PENGUMUMAN' => $timestamp
                );
                
                //insert into database
                $this->pengumuman_model->insertPengumuman($data);
                
                //after all chaos end
                redirect('pengumuman/pengumuman', $sitedata);

                
            }
        }
        else{
            $this->load->view('pengumuman/form_tambah_pengumuman', $sitedata);
        }
    }
    function detail_pengumuman($id_pengumuman){
        echo $id_pengumuman;
    }
}
?>